package com.glxt.controller;

import com.glxt.model.JPABean;
import com.glxt.repository.JPARepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;

@Controller
public class JPAController {
    @Autowired
    JPARepository rep;
    @ResponseBody
    @GetMapping("/JPA_All")
    public List<JPABean> JPA_All(Model m){
        List<JPABean> UserList = rep.findAll();
        m.addAttribute("Result",UserList);
        return UserList;
    }

    @GetMapping("/JPA_All_Page")
    public String JPA_All_Page(Model m){
        List<JPABean> UserList = rep.findAll();
        m.addAttribute("Result",UserList);
        return "result";
    }

    @ResponseBody
    @GetMapping("/JPA_fbID/{id}")
    public Optional<JPABean>fbID(@PathVariable int id, Model m){
        Optional<JPABean> UserList =rep.findById(id);
        m.addAttribute("Result", UserList);
        return UserList;
    }
    @ResponseBody
    @GetMapping("/JPA_fbn/{name}")
    public List<JPABean> fbn(@PathVariable String name){
        return (List<JPABean>) rep.findByUserName(name);
    }
    @ResponseBody
    @GetMapping("/JPA_Role/{role}")
    public List<JPABean> JPA_Role(@PathVariable String role, Model m){
        List<JPABean> UserList = rep.findByuserRole(role);
        m.addAttribute("Result", UserList);
        return UserList;
    }
    @GetMapping("/JPA_lbn/{name}")
    public String lbn(@PathVariable String name,Model m){
        List<JPABean> UserList = rep.listByName(name);
        m.addAttribute("Result", UserList);
        return "result";
    }
    @ResponseBody
    @Transactional
    @GetMapping("/JPA_Insert")
    public String JPAInsert(){
        if(rep.JPAInsert("hesuyang_JPA","123","admin") > 0){
            System.out.println("插入成功");
            return "插入成功";
        }else{
            System.out.println("插入失败");
            return "插入失败";
        }

    }
    @ResponseBody
    @Transactional
    @GetMapping("/JPA_Delete/{name}")
    public String JPADelete(@PathVariable String name){
        rep.JPADelete(name);
        return "删除成功";
    }
    @ResponseBody
    @Transactional
    @GetMapping("/JPA_Update/{name}&{name1}")
    public String JPAUpdate(@PathVariable String name,@PathVariable String name1){
        rep.JPAUp(name,name1);
        return "修改成功";
    }
    @ResponseBody
    @GetMapping("/JPA_Example")
    public List<JPABean> JPA_Example(Model m){

        JPABean user = new JPABean();
        user.setUserName("a");
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("userName", ExampleMatcher.GenericPropertyMatchers.startsWith());
        Example<JPABean> example = Example.of(user,matcher);
        List<JPABean> UserList = rep.findAll(example);
        m.addAttribute("Result", UserList);
        return UserList;
    }
}
